package com.csxy.jqr.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.csxy.jqr.JwtUtils;
import com.csxy.jqr.R;
import com.csxy.jqr.entity.TOrder;
import com.csxy.jqr.exception.MyException;
import com.csxy.jqr.service.OrderService;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/edeorder/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @GetMapping
    public String list()
    {
        return "hello world";
    }
    // 生成订单的方法
    @RequestMapping("/createOrder/{courseId}")
    public R createOrder(@PathVariable("courseId") String courseId, HttpServletRequest request) {
        //从请求头中获取用户id
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        //判断是否登录
        if(StringUtils.isEmpty(memberId)) {
            throw new MyException(20001, "请登录");
        }
        //生成订单，并生成对应的订单号
        String orderNo = orderService.createOrders(courseId, memberId);
        return R.ok().data("orderId", orderNo);
    }
    //根据订单号查询订单信息
    @GetMapping("/getOrderInfo/{orderNo}")
    public R getOrderInfoById(@PathVariable("orderNo") String orderNo) {
        QueryWrapper<TOrder> wrapper = new QueryWrapper<>();
        wrapper.eq("order_no", orderNo);
        TOrder tOrder = orderService.getOne(wrapper);
        return R.ok().data("item", tOrder);
    }
}
